package com.crazymaker.springcloud.user.info.controller;

import com.crazymaker.springcloud.base.service.SysUserServiceImpl;
import com.crazymaker.springcloud.common.constants.SessionConstants;
import com.crazymaker.springcloud.common.result.RestOut;
import com.crazymaker.springcloud.user.info.api.dto.LoginInfoDTO;
import com.crazymaker.springcloud.user.info.api.dto.LoginOutDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "用户端登录与退出", tags = "用户端登录与退出DEMO")
@RestController
@RequestMapping("/api/session")
public class SessionController {

    @Autowired
    private SysUserServiceImpl userService;

    @PostMapping("/login/v1")
    @ApiOperation(value = "用户端登录")
    public RestOut<LoginOutDTO> login(@RequestBody LoginInfoDTO loginInfoDTO, HttpServletRequest request, HttpServletResponse response) {
        LoginOutDTO outDTO = userService.login(loginInfoDTO);
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        response.setHeader(SessionConstants.ADMIN_AUTHORIZATION_HEAD, outDTO.getToken());
        return RestOut.success(outDTO);
    }
}
